package TrueSnake;
import java.util.LinkedList;
import jline.Terminal;
import jline.TerminalFactory;
import jline.console.ConsoleReader;

public class TrueSnake{
    static LinkedList<Pair> snake=new LinkedList<>();
    static char direction='D';
    public static void main(String[] args) throws InterruptedException {
        Pair start=new Pair(4, 4);
        snake.add(start);
        snake.add(new Pair(3,4 ));
        snake.add(new Pair(2, 4));
        startKeyListener();
        while(true){
            snakeMove();
            draw();//画布
            
            // sleep();//等待时间
            System.out.println(direction);
            Thread.sleep(400);
        }
    }
    static void snakeMove(){
        Pair head=snake.getFirst();
        Pair newHead=new Pair(head.x, head.y);
        switch (direction) {
            case 'A':newHead.y--;break;
            case 'D':newHead.y++;break;
            case 'W':newHead.x--;break;
            case 'S':newHead.x++;break;
        }
        if(newHead.x<=0 || newHead.x>=19 || newHead.y<=0 || newHead.y>=29){
            System.out.println("game Over");
            System.exit(0);
        }

        snake.addFirst(newHead);
        snake.removeLast();
    }
    static void draw(){
        System.out.print("\033[H\033[2J");
        for(int i=0;i<20;i++){
            for(int j=0;j<30;j++){
                Pair cur=new Pair(i, j);
                if(i==0||i==19 ||j==0||j==29){
                    System.out.print("#");
                }else if(snake.contains(cur)){
                    System.out.print(snake.getFirst().equals(cur)?'O':'o');
                }else{
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
    static void startKeyListener(){
        new Thread(() -> {
            try {
                Terminal terminal=TerminalFactory.create();
                terminal.init();
                ConsoleReader reader=new ConsoleReader();
                reader.setHandleLitteralNext(true);
                while (true) {
                    int input = reader.readCharacter();
                    char cmd = Character.toUpperCase((char) input);
                    if (input == 27) break;       
                    //方向限制（比如不能从D直接按A）
                    if ((cmd == 'A' && direction != 'D') ||
                        (cmd == 'D' && direction != 'A') ||
                        (cmd == 'W' && direction != 'S') ||
                        (cmd == 'S' && direction != 'W')) {
                        direction = cmd;
                    }
                    System.out.println(cmd);
                }
                terminal.restore();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }
}
    
class Pair{
    int x;
    int y;
    public Pair(int x,int y){
        this.x=x;
        this.y=y;
    }
    @Override
    public boolean equals(Object o){
        if (this==o)return true;
        Pair a=(Pair)o;
        return x==a.x && y==a.y;
    }
}